//
//  LoginViewController.m
//  shike
//
//  Created by RegentSoft on 2017/4/19.
//  Copyright © 2017年 RegentSoft. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "SetPasswordController.h"
@interface LoginViewController (){
    UIView * bgView;//遮罩背景
}
@end
@implementation LoginViewController
-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self httpRequest];
    [self creatView];
}
- (void)newVersion{
    [(AppDelegate*)[UIApplication sharedApplication].delegate setRootVC];
}
-(void)creatView{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UIImageView * image = [[UIImageView alloc] init];
    image.image = [UIImage imageNamed:@"aboutshike"];
    [view addSubview:image];
    
    UIView * userView = [[UIView alloc] init];
    userView.backgroundColor = [UIColor colorWithHexString:@"#F0F0F5"];
    userView.layer.cornerRadius = 4.0;
    [view addSubview:userView];
    
    UIImageView * userImage = [[UIImageView alloc] init];
    userImage.image = [UIImage imageNamed:@"personal1"];
    [userView addSubview:userImage];
    
    UIView * lineView1 =[[UIView alloc] init];
    lineView1.backgroundColor = [UIColor colorWithHexString:@"#999999"];
    [userView addSubview:lineView1];
    
    UITextField * userText = [[UITextField alloc] init];
    userText.textAlignment = NSTextAlignmentLeft;
    userText.font = [UIFont systemFontOfSize:16.0];
    userText.textColor = [UIColor colorWithHexString:@"#c8c8c8"];
    userText.placeholder = @"请输入账号";
    [userView addSubview:userText];
    
    UIView * passwordView = [[UIView alloc] init];
    passwordView.backgroundColor = [UIColor colorWithHexString:@"#F0F0F5"];
    passwordView.layer.cornerRadius = 4.0;
    [view addSubview:passwordView];
    
    UIView * lineView2 =[[UIView alloc] init];
    lineView2.backgroundColor = [UIColor colorWithHexString:@"#999999"];
    [passwordView addSubview:lineView2];
    
    UIImageView * passwordImage = [[UIImageView alloc] init];
    passwordImage.image = [UIImage imageNamed:@"password"];
    [passwordView addSubview:passwordImage];
    
    UITextField * passwordText = [[UITextField alloc] init];
    passwordText.textAlignment = NSTextAlignmentLeft;
    passwordText.font = [UIFont systemFontOfSize:16.0];
    passwordText.textColor = [UIColor colorWithHexString:@"#c8c8c8"];
    passwordText.placeholder = @"请输入密码";
    [passwordView addSubview:passwordText];
    
    UIButton * loginButoon = [[UIButton alloc] init];
    [loginButoon setTitle:@"登录" forState:UIControlStateNormal];
    loginButoon.backgroundColor = [UIColor colorWithHexString:@"#61CAFD"];
    [loginButoon setTintColor:[UIColor blackColor]];
    loginButoon.layer.cornerRadius = 4.0;
    [loginButoon addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:loginButoon];
    
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(110);
        make.left.equalTo(self.view.mas_left).offset((self.view.frame.size.width-150)/2);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(150);
    }];

    [userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(image.mas_bottom).offset(25);
        make.left.equalTo(view.mas_left).offset(15);
        make.right.equalTo(view.mas_right).offset(-15);
        make.height.mas_equalTo(40);
    }];
    [userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userView.mas_top).offset(5);
        make.left.equalTo(userView.mas_left).offset(5);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userView.mas_top).offset(10);
        make.left.equalTo(userImage.mas_right).offset(5);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(20);
    }];
    
    [passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userView.mas_bottom).offset(15);
        make.left.equalTo(view.mas_left).offset(15);
        make.right.equalTo(view.mas_right).offset(-15);
        make.height.mas_equalTo(40);
    }];
    
    [passwordImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwordView.mas_top).offset(5);
        make.left.equalTo(passwordView.mas_left).offset(5);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwordView.mas_top).offset(10);
        make.left.equalTo(passwordImage.mas_right).offset(5);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(20);
    }];
    
    [userText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userView.mas_top).offset(5);
        make.left.equalTo(userImage.mas_right).offset(10);
        make.right.equalTo(userView.mas_right).offset(0);
        make.height.mas_equalTo(30);
    }];
    
    [passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwordView.mas_top).offset(8);
        make.left.equalTo(passwordImage.mas_right).offset(10);
        make.right.equalTo(passwordView.mas_right).offset(0);
        make.height.mas_equalTo(30);
    }];
    
    [loginButoon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwordView.mas_bottom).offset(30);
        make.left.equalTo(userView.mas_left).offset(0);
        make.right.equalTo(userView.mas_right).offset(0);
        make.height.mas_equalTo(40);
    }];
    
}
-(void)loginButtonClick{
//    [self httpRequest];
    bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    bgView.alpha = 0.75;
    [self.view addSubview:bgView];
    UIView * setView = [[UIView alloc] init];
    setView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    setView.alpha = 1.0;
    [bgView addSubview:setView];
    [UIView animateWithDuration:1.0 animations:^{
        [bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left).offset(0);
            make.right.equalTo(self.view.mas_right).offset(0);
            make.top.equalTo(self.view.mas_top).offset(0);
            make.bottom.equalTo(self.view.mas_bottom).offset(0);
        }];
        [bgView layoutIfNeeded];
    } completion:^(BOOL finished) {
    }];
    
    UIImageView * tureImageView = [[UIImageView alloc] init];
    tureImageView.image = [UIImage imageNamed:@"true2"];
    [setView addSubview:tureImageView];
    
    UILabel * tureLabel = [[UILabel alloc] init];
    tureLabel.text = @"登录成功";
    tureLabel.textAlignment = NSTextAlignmentCenter;
    tureLabel.font = [UIFont systemFontOfSize:16.0];
    tureLabel.textColor = [UIColor colorWithHexString:@"#323232"];
    [setView addSubview:tureLabel];
    
    UILabel * describeLabel = [[UILabel alloc] init];
    describeLabel.text = @"为了账号安全，请修改密码";
    describeLabel.textAlignment = NSTextAlignmentCenter;
    describeLabel.font = [UIFont systemFontOfSize:10.0];
    describeLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    [setView addSubview:describeLabel];
    
    UIButton * closeButton =[[UIButton alloc] init];
    closeButton.backgroundColor = [UIColor colorWithHexString:@"#61CAFD"];
    closeButton.layer.cornerRadius = 8.0;
    [closeButton setTitle:@"修改密码" forState:UIControlStateNormal];
    closeButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [closeButton setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(nextSettingView) forControlEvents:UIControlEventTouchUpInside];
    [setView addSubview:closeButton];
    
    [setView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset((DeviceWidth-240)/2);
        make.top.equalTo(self.view.mas_top).offset((DeviceHeight-181)/2);
        make.width.mas_equalTo(240);
        make.height.mas_equalTo(181);
    }];
    [tureImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(setView.mas_left).offset(100);
        make.top.equalTo(setView.mas_top).offset(32);
        make.width.mas_equalTo(36);
        make.height.mas_equalTo(36);
    }];
    [tureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(setView.mas_left).offset(87);
        make.top.equalTo(tureImageView.mas_bottom).offset(12);
        make.width.mas_equalTo(66);
        make.height.mas_equalTo(16);
    }];
    [describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(setView.mas_left).offset(55);
        make.top.equalTo(tureLabel.mas_bottom).offset(17);
        make.width.mas_equalTo(125);
        make.height.mas_equalTo(10);
    }];
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(setView.mas_left).offset(15);
        make.bottom.equalTo(setView.mas_bottom).offset(-15);
        make.width.mas_equalTo(210);
        make.height.mas_equalTo(40);
    }];
}
-(void)nextSettingView{
    [bgView removeFromSuperview];
    [self nextSetPassword];
}
-(void)nextSetPassword{
    SetPasswordController * password = [[SetPasswordController alloc] init];
    password.hidesBottomBarWhenPushed = YES;
    [self .navigationController pushViewController:password animated:YES];
}
-(void)httpRequest{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:@"password" forKey:@"grant_type"];
    [dic setObject:@"3" forKey:@"client_id"];
    [dic setObject:@"usAYjBHUzfmDcQGurzTYXimZjRKGJUbABYw8N7a7" forKey:@"client_secret"];
    [dic setObject:@"13710749821" forKey:@"username"];
    [dic setObject:@"88888888" forKey:@"password"];
    [self loadProgressViewIsShow:YES];
    [[RSNetworkManager sharedManager] POST:@"http://120.77.252.204/oauth/token" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"1111====%@",dic);
        [self loadProgressViewIsShow:NO];
        [self showLoadProgressViewWithMessage:@"获取返回数据异常" delay:2.0];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self loadProgressViewIsShow:NO];
    }];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
